/*
 * Copyright (C) 2017 The MoonLake Authors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


package com.minecraft.moonlake.antistuckportal.listeners;

import com.minecraft.moonlake.antistuckportal.AntiStuckPortal;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityPortalEnterEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerQuitEvent;

public class PlayerListener implements Listener {

    private final AntiStuckPortal main;

    public PlayerListener(AntiStuckPortal main) {
        this.main = main;
    }

    public AntiStuckPortal getMain() {
        return main;
    }

    @EventHandler(priority = EventPriority.LOWEST)
    public void onQuit(PlayerQuitEvent event) {
        // 处理玩家退出事件
        Player player = event.getPlayer();
        // 玩家退出则清除并关闭检测 task
        getMain().getAntiStuckPortalManager().close(player);
    }

    @EventHandler(priority = EventPriority.LOWEST)
    public void onDeath(PlayerDeathEvent event) {
        // 处理玩家死亡事件
        Player player = event.getEntity();
        // 玩家死亡则清除并关闭检测 task
        getMain().getAntiStuckPortalManager().close(player);
    }

    @EventHandler(priority = EventPriority.HIGHEST)
    public void onPortalEnter(EntityPortalEnterEvent event) {
        // 实体进入传送门事件
        Entity entity = event.getEntity();
        if(!(entity instanceof Player)) return; // 实体不是玩家则返回
        // 处理玩家进入传送门事件
        getMain().getAntiStuckPortalManager().handlerPlayerPortalEnter(event, (Player) entity);
    }
}
